#------------------------------------------------------------------------------
rm(list = ls())
library(LalRUtils)
libreq(
  data.table, zoo, tictoc, fixest, PanelMatch, patchwork,
  rio, magrittr, janitor, did, panelView, ggplot2, RPushbullet, ggiplot, 
  tidyverse, data.table, zoo, tictoc, fst, fixest, PanelMatch, patchwork,
  rio, magrittr, janitor, did, panelView, ggiplot, tictoc, binsreg, interflex
)

set.seed(42)
theme_set(lal_plot_theme())

notif = \(x) pbPost("note", x)

#------------------------------------------------------------------------------



#------------------------------------------------------------------------------
# Define Paths
#------------------------------------------------------------------------------

# R studio
setwd( dirname(rstudioapi::getActiveDocumentContext()$path) )
# R default : unccoment if you use default R
# setwd(getSrcDirectory(function(){})[1])
#------------------------------------------------------------------------------



#------------------------------------------------------------------------------
# Load Data
#------------------------------------------------------------------------------

vcf <- fread("vcf_data_complete.csv", sep = ",")
setnames( vcf, "d", "D")
vcf_data <- copy( vcf )
gfc <- fread("gfc_dta.csv" )

#------------------------------------------------------------------------------



#------------------------------------------------------------------------------
# Figure 3: Aggregate trends in Forest Cover Index in VCF data (top) and 
#           Total Deforested Area in GFC (bottom) in the 9 states under study
#------------------------------------------------------------------------------

# Get sample data
ts = vcf_data[, .( avg_tree_cover = mean( forest_index ),
                   deforested = mean( ifelse( forest_index < 1, 1, 0 ) ) ), 
              year ]
gfc_ts = gfc[, .( deforested_area = sum( def_ha ) ), 
             year]

# Generate figure
f1 <- rbind( data.frame( year = 1982:2000, 
                         deforested_area = NA ), 
             gfc_ts ) %>%
  ggplot( aes( year, deforested_area ) ) + 
  geom_point() +
  geom_smooth( se = FALSE ) + 
  labs(y = "Total Deforestation (Hectares)", 
       x = "Year", title = "GFC" )+ 
  theme(axis.text = element_text( size = 18 ), 
        text = element_text( size = 18 ), 
        plot.title = element_text(size = 20))

f2 <- ggplot( ts, aes( year, avg_tree_cover ) ) + 
  geom_point() +
  geom_smooth( se = FALSE ) + 
  labs(y = "Average Tree Canopy Across All Pixels", 
       x = "Year",
       title = "VCF" ) + 
  theme(axis.text = element_text( size = 18 ), 
        text = element_text( size = 18 ), 
        plot.title = element_text(size = 20))

( ff <- f2 / f1 )

# Save figure
ggsave( "main_figure3.pdf",
        device = cairo_pdf, 
        width = 7, height = 11 )

#------------------------------------------------------------------------------
